import history from '../../utils/history';
import {
  REDIRECT_PAGE,
  LIST_DATA,
  LIST_DATA_LV,
  // LIST_SCROLL_POSITION,
} from '../../constants/actionTypes';
import fetch from '../../utils/fetch';

// 跳转页面
export function redirect(url, direction='left', tabHide='true', e) {
  return dispatch => {
    // console.log('ur');
    if(e){
      e.preventDefault();
      e.stopPropagation();
    }
    let redirect = {
      direction,
      tabHide
    };
    dispatch({
      type: REDIRECT_PAGE,
      redirect
    })
    history.push(url);
  }
}

// 获取列表数据
export function getListData(url, parame){
  return dispatch => {
    // return fetch(url)
    //     .then(res => res.json())
    //     .then(json => dispatch({ type: 'LIST_DATA', data: json }));
    // return axios.get(url, {
    //   params: parame
    // })
    // .then((res)=>{
    //   console.log('res',res);
    //   var data = res.data.data;
    //   dispatch({type:LIST_DATA, data})
    // })
    // .catch((error)=>{
    //   console.log(error);
    // });

  }
}

// 列表页面跳转
export function redirectList(url, offsetTop) {
  return dispatch => {
    let redirect = {
      direction: 'left',
      tabHide: true
    };
    dispatch({
      type: REDIRECT_PAGE,
      redirect
    })
    dispatch({type:LIST_DATA_LV, offsetTop})
    history.push(url);
  }
}

// 返回上一页
export function goBack(tabHide=false, url, direction='right') {
  return dispatch => {
    let redirect = {
      direction: direction,
      tabHide: false
    };
    dispatch({
      type: REDIRECT_PAGE,
      redirect
    })
    history.goBack()
  }
}

// 获取数据
export function getData(url, type, parame, timeout){
  return fetch(url, type, parame, timeout)
}
